package com.oa.dao.base;

import java.io.Serializable;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.List;

import javax.annotation.Resource;

import com.oa.utils.PageBean;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Projections;
import org.springframework.orm.hibernate5.support.HibernateDaoSupport;


/**
 * 持久层通用实现
 *
 * @param <T>
 * @author Administrator
 */
public class BaseDaoImpl<T> extends HibernateDaoSupport implements BaseDao<T> {

    // 代表某个实体
    private Class<T> entityClass;

    // spring 注入sessionFactory
    @Resource
    public void setMySessionFactory(SessionFactory sessionFactory) {
        super.setSessionFactory(sessionFactory);
    }

    public BaseDaoImpl() {
        ParameterizedType superclass = (ParameterizedType) this.getClass().getGenericSuperclass();

        Type[] actualTypeArguments = superclass.getActualTypeArguments();

        entityClass = (Class<T>) actualTypeArguments[0];

    }

    public T findById(Serializable id) {

        return this.getHibernateTemplate().get(entityClass, id);
    }

    public T findByIdString(String id) {
        String hql = "FORM Staff where id = ?";
        return (T) this.getHibernateTemplate().find(hql, id);
    }

    public List<T> findAll() {
        String hql = "FROM " + entityClass.getSimpleName();
        return (List<T>) this.getHibernateTemplate().find(hql);
    }

    @Override
    public void save(T entity) {
        this.getHibernateTemplate().save(entity);
    }

    @Override
    public void delete(T entity) {
        this.getHibernateTemplate().delete(entity);
    }

    @Override
    public void update(T entity) {
        this.getHibernateTemplate().update(entity);
    }


    @Override
    public void saveOrUpdate(T entity) {
        this.getHibernateTemplate().saveOrUpdate(entity);
    }

    /**
     * 获取总条数
     */
    public Integer getTotalCount(DetachedCriteria dc) {

        dc.setProjection(Projections.rowCount());

        List<Long> list = (List<Long>) getHibernateTemplate().findByCriteria(dc);

        dc.setProjection(null);

        if (list != null && list.size() > 0) {
            Long count = list.get(0);

            return count.intValue();
        } else {

            return null;
        }
    }

    @Override
    public T findByCriteria(DetachedCriteria dc) {
        List<T> list = (List<T>) getHibernateTemplate().findByCriteria(dc);
        if (list != null && list.size() > 0) {
            return list.get(0);
        }
        return null;
    }

    @Override
    public List<T> findByCriteriaReturnList(DetachedCriteria dc) {
        return (List<T>) getHibernateTemplate().findByCriteria(dc);
    }

    /**
     * 分页查询
     */

    public PageBean pagingQuery(PageBean pageBean) {
        DetachedCriteria dc = pageBean.getCriteria();

        //获取总数
        Integer totalCount = this.getTotalCount(dc);

        pageBean.setCount(totalCount);

        List<T> data = (List<T>) getHibernateTemplate().findByCriteria(dc, pageBean.getStart(), pageBean.getPageSize());

        pageBean.setData(data);

        return pageBean;
    }


}
